package com.app.service.impl;

import com.app.mapper.CategoryMapper;
import com.app.model.CategoryModel;
import com.app.service.CategoryService;
import com.app.utils.FindChildrenUitl;
import com.app.vo.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryModel> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<CategoryModel> categoryPageList(int page, int limit, String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        if (!(name == null || name.equals(""))) {
            queryWrapper.like("name", name);
        }
        queryWrapper.select("id", "pid", "name", "sort", "status").orderByAsc("id").orderByDesc("sort");
        Page<CategoryModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<CategoryModel> categoryList = categoryMapper.selectPage(pages, queryWrapper);
        return categoryList;
    }

    @Override
    public CategoryVo getCategoryList(int page, int limit, String name) {
        Page<CategoryModel> page1 = new Page<>(page, limit);

        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.select("id", "pid", "name", "sort", "status").orderByAsc("id").orderByDesc("sort");
        Page<CategoryModel> categoryPage = categoryMapper.selectPage(page1, qw);

        long count = categoryPage.getTotal();
        List<CategoryModel> categoryList = categoryPage.getRecords();
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setList(categoryList);
        categoryVo.setCount((int) count);
        return categoryVo;
    }

    @Override
    public List<CategoryModel> getCategoryByPid(Integer pid) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").eq("pid", pid);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }


    @Override
    public List<CategoryModel> getCategoryTree(List<CategoryModel> categoryList) {
        List<Integer> arr1 = new ArrayList<>();
        for (int i = 0; i < categoryList.size(); i++) {
            arr1.add(categoryList.get(i).getId());
        }
        Integer ArrId = 1;
        boolean result1 = arr1.contains(ArrId);
        List<Integer> pidArr1 = new ArrayList<>();
        if (!result1) {
            for (int i = 0; i < categoryList.size(); i++) {
                boolean PidResult = arr1.contains(categoryList.get(i).getPid());
                if (!PidResult) {
                    pidArr1.add(categoryList.get(i).getPid());
                }
            }
            pidArr1.add(ArrId);
            arr1.addAll(pidArr1);
            LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(arr1);
            ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
            List<CategoryModel> catModelList1 = new ArrayList<>();
            for (int i = 0; i < listWithoutDuplicates.size(); i++) {
                CategoryModel categoryModel = categoryMapper.selectById(listWithoutDuplicates.get(i));
                catModelList1.add(categoryModel);
            }
            List<CategoryModel> categoryChildren = FindChildrenUitl.findCategoryChildren(categoryList, 0);
            return categoryChildren;
        }
        List<CategoryModel> categoryChildren = FindChildrenUitl.findCategoryChildren(categoryList, 0);
        return categoryChildren;
    }

    @Override
    public void deleteAll(Integer id) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        categoryMapper.delete(queryWrapper);
    }
    //------------------------------以上为项目初始化---------------------
    //------------------------------以下为业务代码-----------------------

    //获取所有商家类型列表
    @Override
    public List<CategoryModel> getBusinessTypeList() {
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.eq("pid", 10);//pid 10商家类型
        qw.orderByAsc("sort");//pid 10商家类型
        return categoryMapper.selectList(qw);
    }

    //获取所有月销售额类型列表
    @Override
    public List<CategoryModel> getMonthlySalesTypeList() {
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.eq("pid", 30);//pid 30月销售额类型
        return categoryMapper.selectList(qw);
    }

    //获取所有商品类型
    @Override
    public List<CategoryModel> getGoodsTypeList() {
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.eq("pid", 50);//pid 50 商品类型
        return categoryMapper.selectList(qw);
    }

    @Override
    public List<CategoryModel> getOrderReason() {
        QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
        qw.eq("pid", 75);
        return categoryMapper.selectList(qw);
    }
}
